package com.hot100.hard;

/**
 * @author zhengTao
 * @version 1.0
 * @description: 寻找两个正序数组的中位数
 * @date 2022/8/5 16:13
 */
public class Demo01 {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int sum = nums1.length + nums2.length;
        if (sum == 0) {
            return 0l;
        } else {
            int[] nums = new int[sum];
            int i = 0, j = 0, k = 0;
            while (i < nums1.length & j < nums2.length) {
                if (nums1[i] < nums2[j]) {
                       nums[k++]=nums1[i++];
                }else {
                       nums[k++]=nums2[j++];
                }
            }
            while (i<nums1.length){
                nums[k++]=nums1[i++];
            }
            while (j<nums2.length){
                nums[k++]=nums2[j++];
            }
            if(sum%2==0){
                int res1=nums[sum/2];
                int res2=nums[sum/2+1];
                double i1 = (res1 + res2) / 2.0;
                return  i1;

            }else {
                return nums[sum/2];
            }
        }


    }
}
